home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / benchmarks / open / open.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-14  |  2.2 KB  |  82 lines

  1. /* 
  2.  * open.c --
  3.  *
  4.  *    This program is a stand-alone benchmark that measures
  5.  *    the cost of opening and closing a file.
  6.  *
  7.  *    open fileName count
  8.  *
  9.  *    where "fileName" is the name of the file to open and
  10.  *    close and "count" tells how many open/close pairs to
  11.  *    execute.
  12.  *
  13.  * Copyright 1989 Regents of the University of California.
  14.  * Permission to use, copy, modify, and distribute this
  15.  * software and its documentation for any purpose and without
  16.  * fee is hereby granted, provided that the above copyright
  17.  * notice appear in all copies.  The University of California
  18.  * makes no representations about the suitability of this
  19.  * software for any purpose.  It is provided "as is" without
  20.  * express or implied warranty.
  21.  */
  22.  
  23. #ifndef lint
  24. static char rcsid[] = "$Header: /sprite/src/benchmarks/open/open.c,v 1.1 89/09/13 20:48:43 ouster Exp $ SPRITE (Berkeley)";
  25. #endif not lint
  26.  
  27.  
  28. #include <stdio.h>
  29. #include <sys/file.h>
  30. #include <sys/time.h>
  31. #include <sys/resource.h>
  32.  
  33. main(argc, argv)
  34. int argc;
  35. char **argv;
  36. {
  37.     int repeats, fd, count;
  38.     double msPer, micros;
  39.     struct rusage begin ,end;
  40.     struct timeval start, stop;
  41.     struct timezone tz;
  42.  
  43.     if (argc != 3) {
  44.     fprintf(stderr, "Usage:  %s fileName count\n",
  45.         argv[0]);
  46.     exit(1);
  47.     }
  48.     repeats = atoi(argv[2]);
  49.  
  50. #ifdef GETRUSAGE
  51.     getrusage(RUSAGE_SELF, &begin);
  52. #else
  53.     gettimeofday(&start, (struct timezone *) NULL);
  54. #endif
  55.  
  56.     for (count = 0 ; count < repeats; count++) {
  57.     fd = open(argv[1], O_RDONLY, 0);
  58.     if (fd < 0) {
  59.         fprintf(stderr, "Couldn't open %s.\n", argv[1]);
  60.         exit(1);
  61.     }
  62.     if (close(fd) != 0) {
  63.         fprintf(stderr, "Error closing %s.\n",
  64.             argv[1]);
  65.         exit(1);
  66.     }
  67.     }
  68. #ifdef GETRUSAGE
  69.     getrusage(RUSAGE_SELF, &end);
  70.     micros = (end.ru_utime.tv_sec + end.ru_stime.tv_sec
  71.         - begin.ru_utime.tv_sec - begin.ru_stime.tv_sec)*1000000
  72.         + (end.ru_utime.tv_usec - begin.ru_utime.tv_usec)
  73.         + (end.ru_stime.tv_usec - begin.ru_stime.tv_usec);
  74. #else
  75.     gettimeofday(&stop, (struct timezone *) NULL);
  76.     micros = 1000000*(stop.tv_sec - start.tv_sec)
  77.         + stop.tv_usec - start.tv_usec;
  78. #endif
  79.     msPer = (micros/repeats/1000.0);
  80.     printf("Time per open/close pair: %.2f milliseconds\n", msPer);
  81. }
  82.